--#include "data\config\boss\StoneTombPigConfig.lua" once
----#include "data\functions\CommonFunc.lua" once

StoneTombPigRankColumn = 
{	
	{"killMon"},				--0，击杀怪物数量
	{"lostMon"},				--1，走失怪物数量
	{"addExp"},					--2，获得经验
	{"lostExp"},				--3，损失经验
}

StoneTombPigRank = Lang.Rank.StoneTombPigRank		--石墓烧猪排行榜，用于存储玩家离线数据


--以下是副本数据
STONETOMB_FUBEN_OWNERLEVEL		= 0				--副本玩家等级
STONETOMB_FUBEN_FOREXPERICE		= 1 			--是否体验副本
STONETOMB_FUBEN_HASFRESHMON		= 2				--是否已经刷怪
STONETOMB_FUBEN_FRESHMONIDX		= 3				--刷出第N个怪
STONETOMB_FUBEN_PUTHELPER1		= 4				--放置的法师的数量
STONETOMB_FUBEN_PUTHELPER2		= 5				--放置的法神的数量
STONETOMB_FUBEN_BUYHELPER1		= 6				--购买的法师的数量
STONETOMB_FUBEN_BUYHELPER2		= 7				--购买的法神的数量


--以下是玩法离线数据
STONETOMB_USERDATA_KILL_MON	= 0				--击杀怪物
STONETOMB_USERDATA_LOST_MON	= 1				--走势怪物
STONETOMB_USERDATA_ADD_EXP	= 2				--获得经验
STONETOMB_USERDATA_LOST_EXP	= 3				--损失经验

--[[
注册一些事件
]]
function StoneTombPigResisterProc()
	--print("ResisterPersonBossProc...")
	--SceneEnterDispatcher.register( StoneTombPigConfig.sceneId, OnEnterStoneTombPig )			--注册进入场景、副本后触发的事件
	SceneExitDispatcher.register( StoneTombPigConfig.sceneId,  OnExitStoneTombPig )				--注册离开场景、副本后触发的事件
	SceneLogoutDispatcher.register( StoneTombPigConfig.sceneId,  OnLogoutStoneTombPig )			--注册从场景、副本中下线的事件
	SceneHandExitDispatcher.register( StoneTombPigConfig.sceneId,  OnHandExitStoneTombPig )		--注册离开场景、副本后触发的事件
end

--[[
进入副本后触发

function OnEnterStoneTombPig( sysarg, sceneId, lastSceneId )
	--print("OnEnterStoneTombPig, sceneId="..sceneId..", lastSceneId="..lastSceneId)
	local dVar 		= Actor.getDyanmicVar(sysarg)
	dVar.buyHelper 	= nil
	dVar.stoneTomeFreshIdx 	= nil
	dVar.stoneTomeEnterTime = System.getCurrMiniTime()
	InitStoneTombPigPanel( sysarg )
	StoneTomePigSendData( sysarg )			--发送一些数据

	StoneTomePigStartNotifyFresh( sysarg )		--刷怪倒计时
end
]]

function InitStoneTombPigRank()
	Rank.Init(StoneTombPigRank, StoneTombPigRank, StoneTombPigRankColumn, 1500, 0)
end

--[[
石墓烧猪副本中恢复上线
]]
function ProcLoginStoneTombPig( sysarg, fubenPtr  )
	--print("ProcLoginStoneTombPig...")
	if not fubenPtr then
		return
	end

	local fubenId 	= Fuben.getFuBenId( fubenPtr )
	local actorId 	= Actor.getActorId( sysarg )
	local actorName	= Actor.getName( sysarg )
	System.sendCommonLog( GameLog.clFubenDropReset_LoginTry, actorId, actorName , 0, 0, fubenId,'','', Lang.ScriptTips.StoneTombLog06 )

	--print("ProcLoginStoneTombPig, fubenId="..fubenId)
	if fubenId ~= StoneTombPigConfig.fubenId then 			--以正式副本为准
		return
	end

	local fubenHandle = Fuben.getFubenHandle( fubenPtr )
	--print("ProcLoginStoneTombPig, fubenHandle="..fubenHandle)
	if fubenHandle <= 0 then
		return
	end

	if not Fuben.isFubenExist( fubenHandle ) then			--副本已经不存在（被撤销）了
		return
	end

	--这里预防曾经有某玩家保存的副本handle正好与此副本相同的情况
	local actorId = Actor.getActorId( sysarg )
	local ownerId = Fuben.getOwnerId( fubenPtr )			--副本Owner
	--print("ProcLoginStoneTombPig, actorId="..actorId..", ownerId="..ownerId)
	if actorId ~= ownerId then
		return
	end
	
	if Actor.enterFuBen(sysarg, fubenHandle, StoneTombPigConfig.sceneId, StoneTombPigConfig.enterPos[1], StoneTombPigConfig.enterPos[2]) then
		InitStoneTombPigPanel( fubenPtr, sysarg )
		StoneTomePigSendData( fubenPtr, sysarg )			--发送一些数据
		System.sendCommonLog( GameLog.clFubenDropReset_LoginOk, actorId, actorName , 0, StoneTombPigConfig.sceneId, fubenId,
			'', '', Lang.ScriptTips.StoneTombLog07 )
	end
end

--[[
石墓烧猪副本中下线
]]
function OnLogoutStoneTombPig( sysarg )
	--print("OnLogoutStoneTombPig...")
	DroplineSaveFubenHandler(sysarg)

	local actorId 	= Actor.getActorId( sysarg )
	local actorName	= Actor.getName( sysarg )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenId 	= Actor.getFubenId( sysarg )
	System.sendCommonLog( GameLog.clFubenDropReset_Logout, actorId, actorName , 0, sceneId, fubenId,'','',
		Lang.ScriptTips.StoneTombLog05 )
end

--[[
离开场景
]]
function OnExitStoneTombPig( sysarg, sceneId, fubenId )
	--print("OnExitStoneTombPig, sceneId="..sceneId..", fubenId="..fubenId)
	CloseActivityRightPanel( sysarg )				--关闭右侧面板
	CloseBoardPanel( sysarg )						--关闭公告牌面板
	local fubenPtr = Actor.getFubenPrt( sysarg )
	local stonePigCfg = GetStoneTombPigCfgOfFuben( fubenPtr )
	if stonePigCfg.forExperience then
		StoneTomePigGetAward( sysarg, 1, false )	--体验副本，退出场景时自动领取第1个奖励,这里必须是false，否则宕机
	end
end

--[[
手工离开场景
注意功能不与OnExitStoneTombPig重复
1、手工离开，即副本打完
2、但是离线数据保存，为了玩家在副本外领取奖励
]]
function OnHandExitStoneTombPig( sysarg )
	--print("OnHandExitStoneTombPig")
	local fubenPtr = Actor.getFubenPrt( sysarg )
	OnStoneTombPigFinish( fubenPtr )					--手工离开，则认为副本打完（但不关闭副本）
	local fubenHandle = Fuben.getFubenHandle( fubenPtr )
	Fuben.setFubenTime( fubenHandle, 1 )				--手工离开，关闭副本
end

--[[
每日数据处理
sVar.stoneTomb 领奖后才清，跨天不清
]]
function StoneTombPigProcDailyData( sysarg )
	--local sVar = Actor.getStaticVar(sysarg)
	--sVar.stoneTomeEnterTimes  = nil
end

--[[
根据是否体验副本，获取配置
]]
function GetStoneTombPigCfgOfFuben( fubenPtr )
	local forExperience = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_FOREXPERICE )
	return GetStoneTombPigCfgByForExperience( forExperience )
end

function GetStoneTombPigCfgByForExperience( forExperience )
	--print("GetStoneTombPigCfgByForExperience, forExperience="..forExperience)
	if forExperience and forExperience > 0 then
		return StoneTombPigConfigForExp
	end
	return StoneTombPigConfig
end

--[[
（活动开始或开启刷怪时）重置数据
]]
function StoneTombPigResetRankData( fubenPtr, actorId )
	--设置损失的经验为全部怪物经验
	local allLostExp = StoneTomePigGetAllMonsterExp( fubenPtr )
	--System.setOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_LOST_EXP, allLostExp )
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_KILL_MON, 0 )				--击杀怪物清0
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_LOST_MON, 0 )				--走失怪物清0
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_ADD_EXP, 0 )				--获得经验清0
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_LOST_EXP, allLostExp )	--损失经验设置为满经验
end

--[[
（领奖后）数据清除
]]
function StoneTombPigClearRankData( actorId )
	--print("ProcLoginStoneTombPig, fubenId="..fubenId)
	--[[
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_KILL_MON, 0 )				--击杀怪物清0
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_LOST_MON, 0 )				--走失怪物清0
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_ADD_EXP, 0 )				--获得经验清0
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_LOST_EXP, 0 )				--损失经验设置清0
	]]
	RemoveCommonRankRecord( actorId, StoneTombPigRank )							--删除该条记录（节省排行榜空间）
end

--扫荡
function SweepStoneTombPig(sysarg)
	if Actor.getIntProperty(sysarg, PROP_ACTOR_SUPER_VIP) == 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig020, ttFlyTip)
		return
	end
	local stonePigCfg = GetStoneTombPigCfgByForExperience()
	if not StoneTomePigCheckEnter(sysarg, stonePigCfg) then
		return
	end
	StoneTomePigBeforeEnter(sysarg, stonePigCfg)

	local actorId 	= Actor.getActorId(sysarg)
	local StoneTombPigCfg 	= GetStoneTombPigCfgOfFuben()
	local monsters 			= StoneTombPigCfg.freshMon[1]
	local allExp 			= 0
	local actorLevel 		= Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	for monIdx, monsterId in ipairs( monsters ) do
		local monsterCfg = StoneTombPigCfg.mobMonster[monsterId]
		if monsterCfg then
			allExp = allExp + GetStoneTombPigExpInLib( actorLevel, monsterCfg.expLibid, monsterCfg.expRate )
		end
	end
	StoneTombPigResetRankData( nil, actorId )
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_ADD_EXP, allExp )
	StoneTomePigOpenAward(sysarg)
	Actor.triggerAchieveEvent(sysarg, aAchieveEventFinishActivity, 15, 1)
end
--[[
进入石墓烧猪副本
]]
function EnterStoneTombPig( sysarg, forExperience )
	--print("EnterStoneTombPig， forExperience="..forExperience)
	local actorId = Actor.getActorId( sysarg )
	--[[因为一些数据，需要脱离玩家和副本而存在，所以需要在引擎创建相关数据结构
	if not System.createOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0065, ttFlyTip)
		return
	end
	]]

	local stonePigCfg = GetStoneTombPigCfgByForExperience( forExperience )
	if CommonEnterScence( sysarg, stonePigCfg, GameLog.clStoneTombPigEnterFee, Lang.ScriptTips.StoneTombLog01, 
		StoneTomePigCheckEnter, StoneTomePigBeforeEnter ) then

		local fubenPtr 		= Actor.getFubenPrt( sysarg )
		local actorLevel 	= Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
		Fuben.setOwnerId( fubenPtr, actorId )
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_OWNERLEVEL, actorLevel )
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_FOREXPERICE, forExperience )
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER1, stonePigCfg.buyHelper[1].freeNum )		--免费的可放置法师数量
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER2, stonePigCfg.buyHelper[2].freeNum )		--免费的可放置法神数量
		--print("EnterStoneTombPig， actorId="..actorId..", actorLevel="..actorLevel)

		--进入副本，重置数据
		StoneTombPigResetRankData( fubenPtr, actorId )
		
		InitStoneTombPigPanel( fubenPtr, sysarg )
		StoneTomePigSendData( fubenPtr, sysarg )			--发送一些数据

		local boardStr = Lang.ScriptTips.StoneTombPig009				--注意：字符串中要带有一个 %d
		OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, GetStoneTombPigCfgOfFuben( fubenPtr ).freshTimeAfterEnter )

		--Actor.unregScriptCallback(sysarg, "StoneTomePigFreshMon")
		Fuben.unregScriptCallback(fubenPtr, "StoneTomePigFreshMon")

		local interval = stonePigCfg.freshTimeAfterEnter * 1000
		--Actor.regScriptTimer2S(sysarg, 0, interval, interval, 1, "StoneTomePigFreshMon")			--一段时间后，执行刷怪1次
		Fuben.regScriptTimer2S(fubenPtr, 0, interval, interval, 1, "StoneTomePigFreshMon")			--一段时间后，执行刷怪1次

		Actor.triggerAchieveEvent(sysarg, aAchieveEventFinishActivity, 15, 1)
	end
end

function StoneTomePigCheckEnter( sysarg, sceneCfg )
	--正式副本要检查进入次数
	if not sceneCfg.forExperience and not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_StoneTomb, 0 ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		return false
	end
	return true
end

function StoneTomePigBeforeEnter( sysarg, sceneCfg )
	--正式副本要增加进入次数
	if not sceneCfg.forExperience then
		AddActorDailyDoneTimes( sysarg, enDailyOpTimes_StoneTomb, 0, 1 )		--进入次数+1
	end
	return true
end

function StoneTomePigFreshMonByHand( sysarg )
	if not IsInStoneTombPigScene( sysarg )	then --要注意此时玩家可能不在副本中
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig007,ttFlyTip)
		return false
	end

	local fubenPtr 	= Actor.getFubenPrt( sysarg )
	StoneTomePigFreshMon( fubenPtr, true )
end

--[[
刷出BOSS，到时或者手工
]]
function StoneTomePigFreshMon( fubenPtr, byHand )
	local actorId 		= Fuben.getOwnerId( fubenPtr )
	local actorPtr 		= Actor.getActorById( actorId )
	local hasFreshMon 	= Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_HASFRESHMON )
	--print("StoneTomePigFreshMon, actorId="..actorId..", hasFreshMon="..hasFreshMon)
	if hasFreshMon > 0 then 		--已经刷出怪物
		if actorPtr then
			Actor.sendTipmsg(actorPtr, Lang.ScriptTips.StoneTombPig006,ttFlyTip)
		end
		return
	end
	Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_HASFRESHMON, 1 )		--设置为已刷怪
	if actorPtr then
		CloseBoardPanel( actorPtr )						--关闭公告牌面板
	end

	Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_FRESHMONIDX, 0 )		--刷怪IDX置0
	--开启刷怪定时器，重置数据（万一重刷，数据也是被清除掉的，玩家占不到便宜）
	StoneTombPigResetRankData( fubenPtr, actorId )

	Fuben.unregScriptCallback(fubenPtr, "StoneTomePigFreshMon")			--终止倒计时定时器
	Fuben.unregScriptCallback(fubenPtr, "FreshStoneTombPigLoop1")		--终止定时器

	local stonePigCfg = GetStoneTombPigCfgOfFuben( fubenPtr )
	Fuben.regScriptTimer2S(fubenPtr, 0, 0, stonePigCfg.freshCd, #stonePigCfg.freshMon[1], "FreshStoneTombPigLoop1")

	if not byHand then 		--没有指定，就算false
		byHand = false
	end
	StoneTombPigOneKeySet( fubenPtr, actorPtr, byHand )			--自动刷一次守卫，以免玩家忘记放守卫了
	Actor.sendTipmsg(actorPtr, Lang.ScriptTips.StoneTombPig013,ttFlyTip)
end


function FreshStoneTombPigLoop1( fubenPtr )
	--print("FreshStoneTombPigLoop1...")
	DoFreshStoneTombPig( fubenPtr, 1 )
end

function FreshStoneTombPigLoop2( sysarg )
	--print("FreshStoneTombPigLoop2...")
	DoFreshStoneTombPig( sysarg, 2 )
end

function DoFreshStoneTombPig( fubenPtr, idx )
	local fubenHandle = Fuben.getFubenHandle( fubenPtr )
	if fubenHandle <= 0 then
		return
	end

	local stonePigCfg 	= GetStoneTombPigCfgOfFuben( fubenPtr )
	local oldFreshIdx	= Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_FRESHMONIDX )
	local newFreshIdx	= oldFreshIdx + 1
	Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_FRESHMONIDX, newFreshIdx )
	local monsterId 	= stonePigCfg.freshMon[idx][newFreshIdx]
	local monsterCfg 	= stonePigCfg.mobMonster[monsterId]
	if monsterCfg then
		freshSceneMonsterInPosForce(monsterCfg, fubenHandle)
	end
	--print("FreshStoneTombPigLoop1, newFreshIdx="..newFreshIdx)
end

function IsInStoneTombPigFuben( fubenPtr, sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	return sceneId == GetStoneTombPigCfgOfFuben( fubenPtr ).sceneId
end

function IsInStoneTombPigScene( sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	return sceneId == StoneTombPigConfig.sceneId or sceneId == StoneTombPigConfigForExp.sceneId
end

--[[
初始化右侧面板
损失经验初始为满经验，每杀一个怪，就减少损失经验
]]
function InitStoneTombPigPanel( fubenPtr, sysarg )
	local actorId = Actor.getActorId( sysarg )
	local killMon, addExp, lostMon, lostExp = StoneTomePigGetResult( actorId )

	local activityType 	= 1 			--副本
	local activityId 	= enRightPanelActivity_StoneTombPig
	local restTime 		= Fuben.getFbTime( fubenPtr )				--副本剩余时间
	local panelInfo 	= {}
	panelInfo.sts 		= 1  										--进行中
	panelInfo.title 	= Lang.ScriptTips.StoneTombName

	panelInfo.timeTitle 	= Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime		= restTime

	panelInfo.contentTitle 	= ""  			--不用显示
	panelInfo.contents 		= {}
	local monKilled = killMon or 0
	local monLose   = lostMon or 0
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont02, monKilled )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont03, monLose )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end

--[[
更新发给玩家
玩家不在线或者不在场景中则不发
]]
function ChangeStoneTombPigPanel( fubenPtr, actorPtr, idx, num, sts )
	--print("ChangeStoneTombPigPanel, idx="..idx..", num="..num..", sts="..sts)
	if not IsInStoneTombPigFuben( fubenPtr, actorPtr ) then
		return
	end

	local panelInfo 	= {}
	panelInfo.sts 		= sts
	panelInfo.contents 	= {}
	if idx == 1 then
		panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont02, num )
	elseif idx == 2 then
		panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont03, num )
	else
		return
	end

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖

	ChangeActivityRightPanel( actorPtr, panelInfo )
end

function OnStoneTombPigMonKillOrLost( fubenPtr, actorPtr, idx, num, sts )
	--print("OnStoneTombPigMonKillOrLost, idx="..idx)
	if idx ~= 1 and idx ~= 2 then
		return
	end
	ChangeStoneTombPigPanel( fubenPtr, actorPtr, idx, num, sts )
end


--[[boss被击杀
--sysarg:归属玩家,玩家不一定在线
--lastHitKiller：最后一击者，不一定是玩家
]]
function OnStoneTombPigMonsterDie(monster, sysarg, monId, lastHitKiller)
	local fubenPtr 	= Actor.getFubenPrt( monster )
	local monsterCfg = GetStoneTomePigMonsterCfgByMonId( fubenPtr, monId )
	if not monsterCfg then
		return
	end
	
	local actorId 	= Fuben.getOwnerId( fubenPtr )
	local actorPtr 	= Actor.getActorById( actorId )

	local killMon, addExp, lostMon, lostExp = StoneTomePigGetResult( actorId )
	local actorLevel = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_OWNERLEVEL )
	--print("OnStoneTombPigMonsterDie, actorId="..actorId..", actorLevel="..actorLevel)
	local getExp 	 = GetStoneTombPigExpInLib( actorLevel, monsterCfg.expLibid, monsterCfg.expRate )		--击杀此怪物获得的经验

	local newKillMon = killMon + 1
	--System.setOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_KILL_MON, newKillMon )
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_KILL_MON, newKillMon )

	local newAddExp = addExp + getExp
	--System.setOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_ADD_EXP, newAddExp )
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_ADD_EXP, newAddExp )

	local newLostExp = lostExp - getExp
	if newLostExp < 0 then
		newLostExp = 0 
	end
	--System.setOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_LOST_EXP, newLostExp )
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_LOST_EXP, newLostExp )
	--print("OnStoneTombPigMonsterDie, monId="..monId..", getExp="..getExp)

	if actorPtr and IsInStoneTombPigScene( actorPtr ) then 		--玩家在线并且在场景中
		local boardStr= string.format( Lang.ScriptTips.panelCont04, newAddExp )
		OpenBoardPanel( actorPtr, enBoardPanelStyle_Default, boardStr )
	end

	local sts = 1
	local stonePigCfg = GetStoneTombPigCfgOfFuben( fubenPtr )
	--if sVar.stoneTomb[1] + sVar.stoneTomb[2] >= #stonePigCfg.freshMon[1] + #stonePigCfg.freshMon[2] then
	if newKillMon + lostMon >= #stonePigCfg.freshMon[1] then 		--副本打完
		OnStoneTombPigFinish( fubenPtr )
		if actorPtr then
			OnStoneTombPigFinishToActor( actorPtr, stonePigCfg )
			sts = 2
		end
	end

	--玩家在线，则通知面板消息
	if actorPtr then
		OnStoneTombPigMonKillOrLost( fubenPtr, actorPtr, 1, newKillMon or 0, sts )
	end
end

--[[
怪物走失
]]
function OnStoneTombPigMonsterTimeout(monster, monId, sceneId, x, y)
	--print("OnStoneTombPigMonsterTimeout, monId="..monId)
	local fubenPtr 	= Actor.getFubenPrt( monster )
	local actorId 	= Fuben.getOwnerId( fubenPtr )
	
	StoneTombPigMonsterTimeout( fubenPtr, actorId, monId )
end

--[[
怪物走失
损失经验从满经验开始，每当击杀一个怪物，就减少所损失的经验
所以走失怪物不增加损失经验
]]
function StoneTombPigMonsterTimeout( fubenPtr, actorId, monId  )
	--print("StoneTombPigMonsterTimeout, monId="..monId)
	local monsterCfg = GetStoneTomePigMonsterCfgByMonId( fubenPtr, monId )
	if not monsterCfg then
		return
	end
	local actorPtr 	= Actor.getActorById( actorId )
	local killMon, addExp, lostMon, lostExp = StoneTomePigGetResult( actorId )

	local actorLevel = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_OWNERLEVEL )
	local missExp = GetStoneTombPigExpInLib( actorLevel, monsterCfg.expLibid, monsterCfg.expRate )

	local newLostMon = lostMon + 1
	--System.setOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_LOST_MON, newLostMon )		--走失怪物+1
	StoneTomePigSetValue( actorId, STONETOMB_USERDATA_LOST_MON, newLostMon )

	--System.setOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_LOST_EXP, lostExp+missExp )		--丢失增加
	--print("StoneTombPigMonsterTimeout, monId="..monId..", missExp="..missExp)

	local sts = 1
	local stonePigCfg = GetStoneTombPigCfgOfFuben( fubenPtr )
	--if sVar.stoneTomb[1] + sVar.stoneTomb[2] >= #stonePigCfg.freshMon[1] + #stonePigCfg.freshMon[2] then
	if killMon + newLostMon >= #stonePigCfg.freshMon[1] then 		--副本打完
		OnStoneTombPigFinish( fubenPtr )
		if actorPtr and IsInStoneTombPigScene( actorPtr ) then 		--玩家在线并且在场景中
			OnStoneTombPigFinishToActor( actorPtr, stonePigCfg )
			sts = 2
		end
	end

	if actorPtr and IsInStoneTombPigScene( actorPtr ) then 		--玩家在线并且在场景中
		OnStoneTombPigMonKillOrLost( fubenPtr, actorPtr, 2, newLostMon or 0, sts )
	end
end

--[[
打怪完毕
]]
function OnStoneTombPigFinish( fubenPtr )
	--print("OnStoneTombPigFinish...")
	Fuben.unregScriptCallback(fubenPtr, "StoneTomePigFreshMon")			--终止倒计时定时器
	Fuben.unregScriptCallback(fubenPtr, "FreshStoneTombPigLoop1")		--终止刷怪定时器
end

--[[
打怪完毕
]]
function OnStoneTombPigFinishToActor( sysarg, stonePigCfg )
	--print("OnStoneTombPigFinishToActor...")
	StoneTomePigOpenAward( sysarg )				--打开奖励结算面板
	if stonePigCfg.quitCountDownTime and stonePigCfg.quitCountDownTime > 0 then
		--print("OnStoneTombPigFinishToActor, quitCountDownTime="..stonePigCfg.quitCountDownTime)
		CloseBoardPanel( sysarg )						--关闭正在打开中的公告牌面板
		Actor.postEntityScriptDelayMsg(sysarg, stonePigCfg.quitCountDownTime*1000, 0, 16,0,0,0,0,false)
		local boardStr = Lang.ScriptTips.COM0054				--注意：字符串中要带有一个 %d
		OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, stonePigCfg.quitCountDownTime )
	end
end

function GetStoneTombPigExpInLib( actorLevel, expLibId, expRate )
	--print("GetStoneTombPigExpInLib, expLibId="..expLibId..", expRate="..expRate)
	return System.getExpFromExpLib(actorLevel, expLibId, expRate, 0)
end

function GetStoneTomePigMonsterCfgByMonId( fubenPtr, monId )
	return GetStoneTombPigCfgOfFuben( fubenPtr ).mobMonster[monId]
end


--[[
法师法神的数量
]]
function GetStoneTombPigHelperNum( fubenPtr )
	local putHelper1 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER1 )
	local buyHelper1 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER1 )

	local putHelper2 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER2 )
	local buyHelper2 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER2 )

	--print("GetStoneTombPigHelperNum, putHelper1="..putHelper1..", buyHelper1="..buyHelper1..", putHelper2="..putHelper2..
	--	", buyHelper2="..buyHelper2)
	return putHelper1, buyHelper1, putHelper2, buyHelper2
end


--[[
一键放
byHand:true-手动放置，2-自动放置
sysarg：注意，自动放置时，玩家可能不在线
]]
function StoneTombPigOneKeySet( fubenPtr, sysarg, byHand )
	if byHand and not IsInStoneTombPigScene( sysarg )	then --要注意此时玩家可能不在副本中
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig007,ttFlyTip)
		return false
	end

	if byHand then 		--手工操作，取玩家当前副本
		fubenPtr = Actor.getFubenPrt( sysarg )
	end
	local stonePigCfg 	= GetStoneTombPigCfgOfFuben( fubenPtr )
	local putHelper1, buyHelper1, putHelper2, buyHelper2 = GetStoneTombPigHelperNum( fubenPtr )
	local lastNum1 = buyHelper1
	local lastNum2 = buyHelper2

	if lastNum1 < 0 then
		lastNum1 = 0
	end

	if lastNum2 < 0 then
		lastNum2 = 0
	end

	local hasPutNum = putHelper1 + putHelper2 		--已经放置的法师和法神数量
	if hasPutNum >= stonePigCfg.buyLimit then 	--已经摆放足够
		if byHand then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig012,ttFlyTip)
		end
		return
	end
	
	if lastNum1 + lastNum2 <= 0 then    --没有购买
		if byHand then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig011,ttFlyTip)
		end
		return
	end

	--print("StoneTombPigOneKeySet, lastNum1="..lastNum1..", lastNum2="..lastNum2..", hasPutNum="..hasPutNum)
	for idx=hasPutNum+1, #stonePigCfg.autoHelperPos do
		--print("StoneTombPigOneKeySet， idx="..idx)
		local  autoPos = stonePigCfg.autoHelperPos[idx]
		if autoPos then
			if lastNum1 > 0 then
				StoneTomePigBuy( fubenPtr, sysarg, 1, autoPos, byHand, false )		--false:循环时不同提示，下面一起提示
				lastNum1 = lastNum1 - 1
			elseif lastNum2 > 0 then
				StoneTomePigBuy( fubenPtr, sysarg, 2, autoPos, byHand, false )
				lastNum2 = lastNum2 - 1
			end
		end
	end

	if sysarg then
		StoneTomePigSendData( fubenPtr, sysarg )		--下发数据	
	end
end

--[[
购买法师或法神，即增加可以放置的次数
]]
function StoneTomePigAddBuyTimes( sysarg, addType )
	--print("StoneTomePigAddBuyTimes, addType="..addType)
	if addType ~= 1 and addType ~= 2 then
		return
	end

	if not IsInStoneTombPigScene( sysarg )	then --要注意此时玩家可能不在副本中
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig007,ttFlyTip)
		return false
	end

	local fubenPtr 		= Actor.getFubenPrt( sysarg )
	local stonePigCfg 	= GetStoneTombPigCfgOfFuben( fubenPtr )
	local buyCfg = stonePigCfg.buyHelper[addType]
	if not buyCfg then
		return
	end

	local putHelper1, buyHelper1, putHelper2, buyHelper2 = GetStoneTombPigHelperNum( fubenPtr )
	if putHelper1 + buyHelper1 + putHelper2 + buyHelper2 >= stonePigCfg.buyLimit then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0046,ttFlyTip)
		return
	end

	if not CheckConsumeCond( sysarg, buyCfg.consume, 1 ) then
		return
	end

	if not DoConsumeCond( sysarg, buyCfg.consume, 1, GameLog.clStoneTombPigBuyMonFee, Lang.ScriptTips.StoneTombLog002 ) then
		return
	end

	if addType == 1 then
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER1, buyHelper1+1 )
	else
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER2, buyHelper2+1 )
	end
	StoneTomePigSendData( fubenPtr, sysarg )		--下发数据
end

--[[
放置法师或法神
setPos ：指定放置坐标
byHand:true-手工放置、false-自动放置
]]
function StoneTomePigBuy( fubenPtr, sysarg, buyType, setPos, byHand, needNotify )
	--print("StoneTomePigBuy, buyType="..buyType)
	if buyType ~= 1 and buyType ~= 2 then
		return
	end

	if byHand and not IsInStoneTombPigScene( sysarg ) then --要注意此时玩家可能不在副本中
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig007,ttFlyTip)
		return false
	end

	if byHand then 		--手工操作，取玩家所在副本ptr
		fubenPtr = Actor.getFubenPrt( sysarg )
	end
	local stonePigCfg 	= GetStoneTombPigCfgOfFuben( fubenPtr )
	local buyCfg 		= stonePigCfg.buyHelper[buyType]
	if not buyCfg then
		return false
	end

	local putHelper  = 0			--已经放置法师或法神的次数
	local buyHelper  = 0			--剩余购买（可放置的）法师或法神的次数
	if buyType == 1 then
		putHelper = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER1 )
		buyHelper = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER1 )
	elseif buyType == 2 then
		putHelper = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER2 )
		buyHelper = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER2 )
	end
	--print("StoneTomePigBuy, putHelper="..putHelper..", buyHelper="..buyHelper)

	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
	local hScene 		= Fuben.getSceneHandleById( stonePigCfg.sceneId, fubenHandle )
	if byHand then 		--手工放置，需要检测是否位置被占用
		local posX, posY 	= Actor.getEntityPosition(sysarg,0,0)
		local scenePtr 		= Fuben.getSceneById( stonePigCfg.sceneId , fubenHandle )
		--local scenePtr 		= Actor.getScenePtr( sysarg )
    	local entityList    = LuaHelp.getNearEntityListFromRange( scenePtr, posX, posY,0,0,1 ) or {}
    	if not setPos then
    		local count 	= table.getn( entityList )
    		if count > 0 then
	    		Actor.sendTipmsg(sysarg,Lang.ScriptTips.StoneTombPig008,ttFlyTip)
				return false
			end
    	end
	end

	if buyHelper <= 0 then
		if byHand then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.StoneTombPig010,ttFlyTip)
		end
		return false
	end

	local newBuyHelper = buyHelper - 1
	if newBuyHelper < 0 then
		newBuyHelper = 0
	end

	if buyType == 1 then
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER1, newBuyHelper )		--可放置次数-1
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER1, putHelper+1 )		--已放置次数+1
	elseif buyType == 2 then
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER2, newBuyHelper )		--可放置次数-1
		Fuben.setFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER2, putHelper+1 )		--已放置次数+1
	end

	if setPos then 		--指定放置坐标（无论手动、自动放置）
		local mon = Fuben.createMonster(hScene, buyCfg.helperMonId, setPos[1], setPos[2], 0)
	else 		--没有指定放置坐标，肯定是手工放置，角色必须在场景内
		local posX, posY = Actor.getEntityPosition(sysarg,0,0)
		local mon = Fuben.createMonster(hScene, buyCfg.helperMonId, posX, posY, 0)
	end

	if fubenPtr and needNotify then
		StoneTomePigSendData( fubenPtr, sysarg )		--下发数据
	end
	return true
end

--[[
领取石墓烧猪奖励
领奖时玩家可能不在副本中
]]
function StoneTomePigGetAward( sysarg, awardIdx, needExitFuben )
	--print("StoneTomePigGetAward, awardIdx="..awardIdx)
	local cfg = StoneTombPigConfig.getAward[awardIdx]	--领奖时玩家可能不在副本中，由于数据会丢失，无法确定是否体验副本，所以按正式副本扣费
	if not cfg then
		return
	end

	local actorId = Actor.getActorId( sysarg )
	local killMon, addExp, lostMon, lostExp = StoneTomePigGetResult( actorId )
	if addExp+lostExp <= 0 then 			--玩家根本没有开始过（这里防止玩家通过外挂连续多次领取）
		--Actor.sendTipmsg( sysarg, Lang.ScriptTips.StoneTombPig019, ttFlyTip+ttChatWindow)
		return
	end

	local allAddExp = 0			--可领取的经验
	if cfg.isLost then
		allAddExp = addExp * cfg.rate
	else
		allAddExp = (addExp + lostExp) * cfg.rate 			--没有损失，经验全给
	end
	
	--print("StoneTomePigGetAward, allAddExp="..allAddExp)
	if cfg.needVip and Actor.getVipGrade(sysarg) < cfg.needVip then
		if cfg.orExpeVip then 		--或者需要体验VIP，
			if not Actor.isInVipExperienceTime( sysarg ) then 		--但不是体验VIP
				Actor.sendTipmsg( sysarg, string.format( Lang.ScriptTips.COM0050, cfg.needVip), ttFlyTip)
				return
			end
		else
			Actor.sendTipmsg( sysarg, string.format( Lang.ScriptTips.COM0050, cfg.needVip), ttFlyTip)
			return
		end
	end

	--[[玩家支付元宝，但实际上没有经验
	这样是防止玩家误以为扣了钱但是没有拿到经验的情况
	]]
	if awardIdx > 1 and allAddExp <= 0 then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.StoneTombPig018, ttFlyTip+ttChatWindow)
		return
	end

	if not CheckConsumeCond( sysarg, cfg.consume,  1 ) then 		--扣除费用
		return
	end

	if not DoConsumeCond( sysarg, cfg.consume, 1, GameLog.clStoneTombPigAwardPay, Lang.ScriptTips.StoneTombLog03 ) then
		return
	end

	local realAddExp = 0 							--实际获得的经验
	local actorLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	local minimumExp = System.getExpFromExpLib( actorLevel, 1, StoneTombPigConfig.miniExpLibRate, 0) 	--计算保底经验
	if allAddExp < minimumExp then   		--全部经验少于保底经验
		realAddExp = minimumExp				--实际经验是保底经验
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.StoneTombPig014, realAddExp), ttFlyTip+ttChatWindow)
	else
		realAddExp = allAddExp				--实际经验是打怪经验
	end

	
	Actor.sendTipmsg( sysarg, string.format( Lang.ScriptTips.StoneTombPig015, killMon, lostMon, addExp, lostExp ), ttChatWindow)
	StoneTombPigClearRankData( actorId )			--领奖时，先清除该用户的离线数据，再加经验，以免重复领取

	AddExpToActor(sysarg, realAddExp, GameLog.clStoneTombPigAwardGet )
	Actor.sendTipmsg( sysarg, string.format( Lang.ScriptTips.StoneTombPig016, awardIdx, realAddExp ), ttChatWindow)

	local actorName = Actor.getName( sysarg )
	System.sendCommonLog( GameLog.clStoneTombPigAddExp, actorId, actorName, awardIdx, realAddExp, addExp,'','', 
		Lang.ScriptTips.StoneTombLog08)
	--print("StoneTomePigGetAward, GameLog.clStoneTombPigAddExp="..GameLog.clStoneTombPigAddExp)

	--System.deleteOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb )		--领奖后该用户的离线数据要删除
	Actor.closeDialogs( sysarg, enLinkStoneTombPigAwardWnd)

	local sceneId = Actor.getSceneId( sysarg )
	if needExitFuben and IsInStoneTombPigScene( sysarg ) then  		--在石墓烧猪副本中
		Actor.exitFuben( sysarg )
	end
end

--[石墓烧猪]下发数据
function  StoneTomePigSendData( fubenPtr, sysarg )
	local fubenPtr 		= Actor.getFubenPrt(sysarg)
	local stonePigCfg 	= GetStoneTombPigCfgOfFuben( fubenPtr )
	local buyCfg1 = stonePigCfg.buyHelper[1]
	if not buyCfg1 then
		return
	end

	local buyCfg2 = stonePigCfg.buyHelper[2]
	if not buyCfg2 then
		return
	end

	local buyHelper1 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER1 )
	local buyHelper2 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_BUYHELPER2 )
	local putHelper1 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER1 )
	local putHelper2 = Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_PUTHELPER2 )
	--print("StoneTomePigSendData, buyHelper1="..buyHelper1..", buyHelper2="..buyHelper2)
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sStoneTomePigSendData)
	if npack then
		DataPack.writeChar(npack, buyHelper1)
		DataPack.writeChar(npack, buyHelper2)
		DataPack.writeChar(npack, putHelper1)
		DataPack.writeChar(npack, putHelper2)
		--print("StoneTomePigSendData, buyHelper1="..buyHelper1..", buyHelper2="..buyHelper2..", putHelper1="..putHelper1..", putHelper2="..putHelper2)
		DataPack.flush(npack)
	end
end


function StoneTomePigGetResult( actorId )
	--[[
	local killMon 	= System.getOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_KILL_MON )
	local lostMon 	= System.getOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_LOST_MON )
	local addExp 	= System.getOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_ADD_EXP )
	local lostExp 	= System.getOfflineUserFuncData( actorId, enOfflineUserFunc_StoneTomb, STONETOMB_USERDATA_LOST_EXP )
	]]
	local killMon 	= StoneTomePigGetValue( actorId, STONETOMB_USERDATA_KILL_MON )
	local lostMon 	= StoneTomePigGetValue( actorId, STONETOMB_USERDATA_LOST_MON )
	local addExp 	= StoneTomePigGetValue( actorId, STONETOMB_USERDATA_ADD_EXP )
	local lostExp 	= StoneTomePigGetValue( actorId, STONETOMB_USERDATA_LOST_EXP )
	--print("StoneTomePigGetResult, killMon="..killMon..", lostMon="..lostMon..", addExp="..addExp..", lostExp="..lostExp)
	return killMon, addExp, lostMon, lostExp
end



--[石墓烧猪]弹出领奖面板
function  StoneTomePigOpenAward( sysarg )
	local actorId = Actor.getActorId( sysarg )
	if actorId <= 0 then
		return
	end

	local killMon, addExp, lostMon, lostExp = StoneTomePigGetResult( actorId )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sStoneTomePigOpenAward)
	if npack then
		DataPack.writeShort(npack, killMon)
		DataPack.writeUInt(npack, addExp)
		DataPack.writeShort(npack, lostMon)
		DataPack.writeUInt(npack, lostExp)
		--print("StoneTomePigOpenAward, killMon="..killMon..", addExp="..addExp..", lostMon="..lostMon..", lostExp="..lostExp)
		DataPack.flush(npack)
	end
	--Actor.sendTipmsg( sysarg, string.format( Lang.ScriptTips.StoneTombPig017, killMon, lostMon, addExp, lostExp ), ttChatWindow)
end

function StoneTomePigGetAllMonsterExp( fubenPtr )
	local StoneTombPigCfg 	= GetStoneTombPigCfgOfFuben( fubenPtr )
	local monsters 			= StoneTombPigCfg.freshMon[1]
	local allExp 			= 0
	local actorLevel 		= Fuben.getFbValue( fubenPtr, STONETOMB_FUBEN_OWNERLEVEL )
	for monIdx, monsterId in ipairs( monsters ) do
		local monsterCfg = StoneTombPigCfg.mobMonster[monsterId]
		if monsterCfg then
			allExp = allExp + GetStoneTombPigExpInLib( actorLevel, monsterCfg.expLibid, monsterCfg.expRate )
		end
	end
	--print("StoneTomePigGetAllMonsterExp, allExp="..allExp)
	return allExp
end

--[[
设置排行榜的值
]]
function StoneTomePigGetValue( actorId, columnIdx )
	return GetCommonRankColumnValueById(actorId, StoneTombPigRank, columnIdx) or 0
end

--[[
获取排行榜的值
]]
function StoneTomePigSetValue( actorId, columnIdx, newNum )
	return SetCommonRankColumnValueById(actorId, StoneTombPigRank, 0, columnIdx, newNum)
end


--InitStoneTombPigRank()					--初始化排行榜
InitCommonRank( StoneTombPigRank, StoneTombPigRankColumn, 1500 )	--初始化排行榜
StoneTombPigResisterProc()				--注册一些事件

